﻿using Abp.Zero.EntityFramework;
using SingleUserMall.Actives;
using SingleUserMall.Adverts;
using SingleUserMall.Area;
using SingleUserMall.Article;
using SingleUserMall.Authorization.Roles;
using SingleUserMall.Authorization.Users;
using SingleUserMall.BargainItemDetail;
using SingleUserMall.Commissions;
using SingleUserMall.Complains;
using SingleUserMall.Coupons;
using SingleUserMall.Dividends;
using SingleUserMall.Headline;
using SingleUserMall.Items;
using SingleUserMall.Logistics;
using SingleUserMall.Mall;
using SingleUserMall.MultiTenancy;
using SingleUserMall.Orders;
using SingleUserMall.Points;
using SingleUserMall.Refunds;
using SingleUserMall.ShoppingCarts;
using SingleUserMall.Sign;
using SingleUserMall.Sms;
using SingleUserMall.Stores;
using SingleUserMall.StoreSettings;
using SingleUserMall.Videos;
using SingleUserMall.Workers;
using System.Data.Common;
using System.Data.Entity;

namespace SingleUserMall.EntityFramework
{
    public class SingleUserMallDbContext : AbpZeroDbContext<Tenant, Role, User>
    {
        //TODO: Define an IDbSet for your Entities...
        public virtual IDbSet<UserAddress> UserAddresses { get; set; }
        public virtual IDbSet<Banner> Banners { get; set; }
        public virtual IDbSet<Coupon> Coupons { get; set; }
        public virtual IDbSet<UserCoupon> UserCoupons { get; set; }
        public virtual IDbSet<UserCommission> UserCommissions { get; set; }
        public virtual IDbSet<UserCash> UserCashes { get; set; }
        public virtual IDbSet<Item> Items { get; set; }
        public virtual IDbSet<ItemCategory> ItemCategories { get; set; }
        public virtual IDbSet<ItemComment> ItemComments { get; set; }
        public virtual IDbSet<ItemSpec> ItemSpecs { get; set; }
        public virtual IDbSet<ItemSkuCategory> ItemSkuCategories { get; set; }
        public virtual IDbSet<ItemSku> ItemSkus { get; set; }
        public virtual IDbSet<RecommendCategory> RecommendCategories { get; set; }
        public virtual IDbSet<Order> Orders { get; set; }
        public virtual IDbSet<OrderDetail> OrderDetails { get; set; }
        public virtual IDbSet<OrderStatusHistory> OrderStatusHistories { get; set; }
        //public virtual IDbSet<ShippingCategory> ShippingCategories { get; set; }
        public virtual IDbSet<Refund> Refunds { get; set; }
        public virtual IDbSet<RefundLog> RefundLogs { get; set; }
        public virtual IDbSet<ShoppingCart> ShoppingCarts { get; set; }
        public virtual IDbSet<Store> Stores { get; set; }
        public virtual IDbSet<GroupOrder> GroupOrders { get; set; }
        public virtual IDbSet<GroupOrderDetail> GroupOrderDetails { get; set; }
        public virtual IDbSet<UserSale> UserSales { get; set; }
        public virtual IDbSet<UserRefit> UserRefits { get; set; }
        public virtual IDbSet<UserShare> UserShares { get; set; }
        public virtual IDbSet<UserFavorite> UserFavorites { get; set; }
        public virtual IDbSet<UserCard> UserCards { get; set; }
        public virtual IDbSet<UserPromotion> UserPromotions { get; set; }
        public virtual IDbSet<UserCode> UserCodes { get; set; }
        public virtual IDbSet<UserWalletDetail> UserWalletDetails { get; set; }
        public virtual IDbSet<UserStoreFavorite> UserStoreFavorite { get; set; }
        public virtual IDbSet<Worker> Workers { get; set; }

        public virtual IDbSet<Area.Area> area { get; set; }
        public virtual IDbSet<Advert> Advert { get; set; }
        public virtual IDbSet<AdvertRecord> AdvertRecord { get; set; }
        public virtual IDbSet<HeadLines> HeadLines { get; set; }
        public virtual IDbSet<UserRedPacketDetail> UserRedPacketDetail { get; set; }
        public virtual IDbSet<Complain> Complain { get; set; }
        public virtual IDbSet<BusinessScope> BusinessScope { get; set; }
        public virtual IDbSet<Commission> Commission { get; set; }
        public virtual IDbSet<StoreSetting> StoreSetting { get; set; }
        public virtual IDbSet<SmsEntity> SmsEntity { get; set; }
        public virtual IDbSet<AdvertSetting> AdvertSetting { get; set; }
        public virtual IDbSet<HomeSetting> HomeSetting { get; set; }
        public virtual IDbSet<StoreBalanceRecord> StoreBalanceRecord { get; set; }
        public virtual IDbSet<StoreRegister> StoreRegister { get; set; }
        public virtual IDbSet<ItemBargain> ItemBargain { get; set; }
        public virtual IDbSet<Dividend> Dividends { get; set; }
        public virtual IDbSet<Video> Videos { get; set; }
        public virtual IDbSet<Articles> Articles { get; set; }
        public virtual IDbSet<BargainItemDetails> BargainItemDetails { get;set;}
        public virtual IDbSet<LogisticsEntity> LogisticsEntity { get; set; }
        public virtual IDbSet<Signed> Signed { get; set; }
        public virtual IDbSet<UserPoints> UserPoints { get; set; }
        public virtual IDbSet<PointRules> PointRules { get; set; }
        public virtual IDbSet<ReleasePointRules> ReleasePointRules { get; set; }
        public virtual IDbSet<Active> Active { get; set; }
        public virtual IDbSet<ActiveRecord> ActiveRecord { get; set; }
        public virtual IDbSet<BargainRecord> BargainRecord { get; set; }

        /* NOTE: 
         *   Setting "Default" to base class helps us when working migration commands on Package Manager Console.
         *   But it may cause problems when working Migrate.exe of EF. If you will apply migrations on command line, do not
         *   pass connection string name to base classes. ABP works either way.
         */
        public SingleUserMallDbContext()
            : base("Default")
        {

        }

        /* NOTE:
         *   This constructor is used by ABP to pass connection string defined in SingleUserMallDataModule.PreInitialize.
         *   Notice that, actually you will not directly create an instance of SingleUserMallDbContext since ABP automatically handles it.
         */
        public SingleUserMallDbContext(string nameOrConnectionString)
            : base(nameOrConnectionString)
        {

        }

        //This constructor is used in tests
        public SingleUserMallDbContext(DbConnection existingConnection)
         : base(existingConnection, false)
        {

        }

        public SingleUserMallDbContext(DbConnection existingConnection, bool contextOwnsConnection)
         : base(existingConnection, contextOwnsConnection)
        {

        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<ItemSpec>().HasRequired(u => u.Item)
                .WithMany(d => d.ItemSpecs)
                .HasForeignKey(c => c.ItemId)
                .WillCascadeOnDelete(false);
            modelBuilder.Entity<OrderDetail>().HasRequired(u => u.Order)
                .WithMany(d => d.OrderDetails)
                .HasForeignKey(c => c.OrderId)
                .WillCascadeOnDelete(false);
            modelBuilder.Entity<Order>().HasRequired(u => u.User)
                .WithMany(d => d.Orders)
                .HasForeignKey(c => c.UserId)
                .WillCascadeOnDelete(false);
            base.OnModelCreating(modelBuilder);

        }
    }
}
